package cn.mgr.logistics.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
import cn.mgr.logistics.dao.impl.SubmitStateRunnable;
import cn.mgr.logistics.model.UserInfo;
import cn.mgr.logistics.util.Global;
import cn.mgr.logistics.util.MyApplication;
import cn.mgr.logistics.util.Utils;

/**
 * Navigation board
 * 
 * @author paddy 2013-3-14����8:21:57
 * @function
 */
public class MainTabActivity extends TabActivity implements OnCheckedChangeListener {

	private TabHost mTabHost;
	private Intent mAIntent;
	private Intent mBIntent;
	private Intent mCIntent;
	private Intent mDIntent;
	private Intent mEIntent;
	private SubmitStateRunnable ssr;
	SharedPreferences sp;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.maintabs);
		MyApplication.getInstance().addActivity(this);
		this.mAIntent = new Intent(this, SummaryActivity.class);
		this.mBIntent = new Intent(this, SearchActivity.class);
		this.mCIntent = new Intent(this, ReleaseActivity.class);
		this.mDIntent = new Intent(this, CustomizeActivity.class);
		this.mEIntent = new Intent(this, MoreActivity.class);
		ssr = new SubmitStateRunnable(this);

		((RadioButton) findViewById(R.id.radio_button0)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button1)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button2)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button3)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button4)).setOnCheckedChangeListener(this);

		setupIntent();
		sp = getSharedPreferences("accountInfo", MODE_PRIVATE);
		Global.loginedUser = new UserInfo();

		Global.loginedUser.setName(sp.getString("username", ""));
		Global.loginedUser.setPassword(sp.getString("password", ""));
		Global.loginedUser.setR_mobile(sp.getString("mobile", ""));
		Global.loginedUser.setPasskey(sp.getString("passkey", ""));
		Global.loginedUser.setR_code(sp.getString("R_code", ""));
		Global.loginedUser.setMember(sp.getBoolean("isMember", false));
		Global.logined = sp.getBoolean("logined", false);

		System.out.println(sp.getString("username", "") + "->" + sp.getString("password", "") + "->"
				+ sp.getBoolean("logined", false) + "-->" + sp.getString("passkey", "") + "-->"
				+ sp.getString("R_code", "") + "-->" + sp.getBoolean("isMember", false));

		if (Utils.detectNetAvailable(this) && Global.logined) {
			Global.isOnline = 1;
			Global.status = true;
			new Thread(ssr).start();
		}

	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radio_button0:
				this.mTabHost.setCurrentTabByTag("A_TAB");
				break;
			case R.id.radio_button1:
				this.mTabHost.setCurrentTabByTag("B_TAB");
				break;
			case R.id.radio_button2:
				this.mTabHost.setCurrentTabByTag("C_TAB");
				break;
			case R.id.radio_button3:
				this.mTabHost.setCurrentTabByTag("D_TAB");
				break;
			case R.id.radio_button4:
				this.mTabHost.setCurrentTabByTag("MORE_TAB");
				break;
			}
		}

	}

	private void setupIntent() {
		this.mTabHost = getTabHost();
		TabHost localTabHost = this.mTabHost;

		localTabHost.addTab(buildTabSpec("A_TAB", R.string.summary, R.drawable.icon_1_n, this.mAIntent));

		localTabHost.addTab(buildTabSpec("B_TAB", R.string.search, R.drawable.tab_search_normal, this.mBIntent));

		localTabHost.addTab(buildTabSpec("C_TAB", R.string.release, R.drawable.tab_message_normal, this.mCIntent));

		localTabHost.addTab(buildTabSpec("D_TAB", R.string.commission, R.drawable.icon_4_n, this.mDIntent));

		localTabHost.addTab(buildTabSpec("MORE_TAB", R.string.more, R.drawable.icon_5_n, this.mEIntent));

	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon, final Intent content) {
		return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel), getResources().getDrawable(resIcon))
				.setContent(content);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return Utils.quitMyApp(keyCode, event, this);
	}

}